package socket.client;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.Socket;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class Write extends Activity {
	private String html = "";
    private Handler mHandler;
 
    private Socket socket=null;
    private String name;
    private BufferedReader networkReader;
    private ObjectOutputStream networkWriter = null;
    private String SevIp = "192.168.0.2"; // SevIp
    private int SevPort = 9007; // SevPort
    
    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        try {
			networkWriter.close();
        	if(socket!=null) 	socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_write);
        mHandler = new Handler();
        
        final EditText et = (EditText) findViewById(R.id.et_write);
        Button btn = (Button) findViewById(R.id.bt_send);
        
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                if (et.getText().toString() != null
                        || !et.getText().toString().equals("")) {
                	//PrintWriter out = new PrintWriter(networkWriter, true);
                	try {
                		setSocket(SevIp, SevPort);
                		Message msg;
                		msg = new Message(et.getText().toString());
                		networkWriter.writeObject(msg);
	                	networkWriter.flush();
						//String return_msg = et.getText().toString();
						//String line = networkReader.readLine();
						//Log.w(line,"Server recieved");
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
                	onBackPressed();
                }
            }
        });
    }
    
    private Thread checkUpdate = new Thread() {
        public void run() {
            try {
                String line;
                Log.w("ChattingStart", "Start Thread");
                while (true) {
                    //Log.w("Chanting is running", "chanting is running");
                    line = networkReader.readLine();
                    html = line;
                    if(!socket.isConnected()){
                        Log.w("socket is disconneted","isConneted");
                    }
                    else{
                        Log.w("socket is conneted","isConneted");
                    }
                    //mHandler.post(showUpdate);
                }
 
            } catch (Exception e) {
            }
        }
    };
    /*
    private Runnable showUpdate = new Runnable() {
        public void run() {
            Toast.makeText(activity_write.this, "Coming word: " + html,
                    Toast.LENGTH_SHORT).show();
        }
    };*/
 
    public void setSocket(String ip, int port) throws IOException {
        try {
            socket = new Socket(ip, port);
            networkWriter =new ObjectOutputStream(socket.getOutputStream());
            //networkReader =new BufferedReader(new InputStreamReader(socket.getInputStream()));
            
        } catch (IOException e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_write, menu);
        return true;
    }
}
